﻿#include "roundprogressbar.h"
#include<QPropertyAnimation>
#include<QPen>
#include<QPainter>
#include<QDebug>
RoundProgressBar::RoundProgressBar(QWidget *parent):QWidget(parent)
  ,m_value(0),m_min(0)
{
    m_max=100;
    m_startAngler=90;
    m_outBarwidth=10;
    m_inBarwidth=8;
    m_inColor=QColor(232,241,240);
    m_outColor=QColor(38,155,175);
    setValue(0);
}
void RoundProgressBar::setInColor(const QColor &col){
    m_inColor=col;
}
void RoundProgressBar::setOutColor(const QColor &col){
    m_outColor=col;
}
void RoundProgressBar::setValue(float val){
    QPropertyAnimation* an=new QPropertyAnimation(this,"m_value");
    an->setStartValue(m_value);
    an->setEndValue(val);
    an->setDuration(2000);
    an->start();
}
void RoundProgressBar::setMValue(float val){
    m_value=val;
    repaint();
}
float RoundProgressBar::getMValue(){
    return  m_value;
}

void RoundProgressBar::resizeEvent(QResizeEvent *){
    int min=qMin(this->width(),this->height());
    m_sqaurepoint=m_outBarwidth;
    m_sqaureside=min-m_outBarwidth;
}
void RoundProgressBar::paintEvent(QPaintEvent *event){
    QPainter p(this);
    PaintOutterBar(p);
    PaintInnerBar(p);
    PaintText(p);

}
void RoundProgressBar::PaintOutterBar(QPainter &painter){
    QPen pen;
    pen.setWidth(m_outBarwidth);
    pen.setColor(m_outColor);
    QRectF rect(m_sqaurepoint,m_sqaurepoint,m_sqaureside,m_sqaureside);
    painter.setPen(pen);
    painter.drawEllipse(rect);
}
void RoundProgressBar::PaintInnerBar(QPainter &painter){
    QPen pen;
    pen.setWidth(m_inBarwidth);
    pen.setColor(m_inColor);
    QRectF rect(m_sqaurepoint,m_sqaurepoint,m_sqaureside,m_sqaureside);
    painter.setPen(pen);

    int startangle=m_startAngler*16;
    int angle=(m_value-m_min)/(m_max-m_min)*16*360*(-1);
    painter.drawArc(rect,startangle,angle);
}
void RoundProgressBar::PaintText(QPainter &painter){
    QFont f("微软雅黑",22,75);
    painter.setPen(m_inColor);
    painter.setFont(f);
    painter.drawText(m_sqaurepoint,m_sqaurepoint,m_sqaureside,m_sqaureside,Qt::AlignHCenter,QString::number(m_value/m_max*100,'f',0)+"%");
}
